package rai.form;

import java.util.ArrayList;
import java.util.List;

import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.log4j.Logger;

import rai.util.Constants;
/**
 * Login Form.
 * @author Tien
 *
 */
public class LoginForm {
    /** ID-txtUserName-txtPassword-Role property. */
    private String ID;
    private String txtUserName;
    private String txtPassword;
    private String Role;

    /** @return the ID */
    public final String getId() {
        return ID;
    }

    /** @return txtUserName */
    public final String getUserName() {
        return txtUserName;
    }

    /** @return txtUserName */
    public final String getPassword() {
        return txtPassword;
    }

    /** @return Role */
    public final String getRole() {
        return Role;
    }

    /**
     * @param EmpID
     *            the ID to set
     */
    public final void setId(final String EmpID) {
        this.ID = EmpID;
    }

    /**
     * @param name
     *            the UserName to set
     */
    public final void setUserName(final String name) {
        this.txtUserName = name;
    }

    /**
     * @param pass
     *            the Password to set
     */
    public final void setPassword(final String pass) {
        this.txtPassword = pass;
    }

    /**
     * @param Role
     *            the Role to set
     */
    public final void setRole(final String Role) {
        this.Role = Role;
    }

    /** Log property */
    private static Logger log = Logger.getLogger("LoginForm");
    
    /**
     * check whenever Password does not have the right type.
     *
     * @param Pass
     * @return error
     */
    public final String checkPass(final String pass) {
        String error = null;

        for (int i = 0; i < pass.length(); i++) {
            if (pass.charAt(i) >= '0' && pass.charAt(i) <= '9') {
                continue;
            } else if (pass.charAt(i) >= 'a' && pass.charAt(i) <= 'z') {
                continue;
            } else if (pass.charAt(i) >= 'A' && pass.charAt(i) <= 'Z') {
                continue;
            } else {
                error = "Password only contains numbers and letters";
                break;

            }
        }

        return error;
    }
    
    /**
     * Validate the User information 's input.
     *
     * @param request
     * @param response
     * @return
     */
    public final List<String> validateLogin(final RenderRequest request,
            final RenderResponse response) {
        log.debug("validateLogin.start");
        final List<String> errorMess = new ArrayList<String>();
        // check whenever the UserName is blank
        if (this.txtUserName.equals(Constants.BLANK_VALUE)) {
            errorMess.add("Please input the Username!");
            log.debug("validate.errorMess: Please input the Username!");
        } else {
            request.getPortletSession().setAttribute(
                    Constants.NAME_PORT_SESS_ATT, this.txtUserName,
                    PortletSession.APPLICATION_SCOPE);
        }
        // check whenever the Password is blank
        if (this.txtPassword.equals(Constants.BLANK_VALUE)) {
            errorMess.add("Please input the Password!");
            log.debug("validate.errorMess: Please input the Password!");
        }
        // check whenever the Password does not match the input 's requirement
        else if (checkPass(txtPassword) != null) {
            errorMess.add(checkPass(txtPassword));
            log
                    .debug("validate.errorMess: Password does not match the requirement!");
        }
        log.debug("validateLogin.end");
        return errorMess;
    }

}
